package run.siyuan.service;


import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import run.siyuan.mapper.SettlementBalanceRecordMapper;
import run.siyuan.model.CommissionSettlement;
import run.siyuan.model.MemberAmount;
import run.siyuan.model.SettlementBalanceRecord;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjj
 * date 2021年05月27日
 */
@Service
public class SettlementBalanceRecordService {

    @Autowired
    private SettlementBalanceRecordMapper settlementBalanceRecordMapper;

    /**
     * 保存结算钱包记录
     *
     * @param memberAmount
     * @param commissionSettlements
     */
    public List<Long> saveSettlementBalanceRecord(MemberAmount memberAmount, List<CommissionSettlement> commissionSettlements) {
        long current = DateUtil.current();
        List<Long> commissionSettlementIds = new ArrayList<>(commissionSettlements.size());
        commissionSettlements.forEach(commissionSettlement -> {
            commissionSettlementIds.add(commissionSettlement.getId());
            SettlementBalanceRecord record = new SettlementBalanceRecord();
            record.setMemberId(memberAmount.getMemberId());
            record.setMemberAmountId(memberAmount.getId());
            record.setType(0);
            record.setRelationId(commissionSettlement.getCommissionBillingId());
            record.setDeleted(0);
            record.setCreateTime(current);
            record.setUpdateTime(current);
            settlementBalanceRecordMapper.insert(record);
        });
        return commissionSettlementIds;
    }

}
